package day02;
//静态同步方法 和同步方法区别
public class Demo2 {
    public synchronized void AAA(){
        String name=Thread.currentThread().getName();
        System.out.println(name +"开始执行A方法");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name+"结束执行A方法");
    }
    public synchronized void BBB(){
        String name=Thread.currentThread().getName();
        System.out.println(name +"开始执行B方法");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name+"结束执行B方法");
    }
    public static synchronized void CCC(){
        String name=Thread.currentThread().getName();
        System.out.println(name +"开始执行C方法");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name+"结束执行C方法");
    }
//    public static synchronized void DDD(){
//        String name=Thread.currentThread().getName();
//        System.out.println(name +"开始执行D方法");
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println(name+"结束执行D方法");
//    }
    public static void main(String[] args) {
        Demo2 d=new Demo2();
        new Thread(()->{
            d.AAA();
        },"A").start();
        new Thread(()->{
            d.BBB();
        },"B").start();
        new Thread(()->{
            Demo2.CCC();
        },"C").start();
        new Thread(()->{
            synchronized (d){
                String name=Thread.currentThread().getName();
                System.out.println(name+"开始执行同步代码块");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(name+"结束执行同步代码块");
            }
        },"D").start();
        new Thread(()->{
            synchronized (Demo2.class){
                String name=Thread.currentThread().getName();
                System.out.println(name+"开始执行同步代码块");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(name+"结束执行同步代码块");
            }
        },"E").start();
    }

}
